﻿hpxAdminApp.controller('licenceSendController', function ($scope, $rootScope, $state, $stateParams, Upload, FILE_URL, PHOTO_FILE_URL, $window, API_URL, NgTableParams, enterpriseService, payingService, fileService) {
    $scope.tableParams = new NgTableParams({}, {
        getData: function (params) {
            var isAlive = 7;
            var keyword = '';
            return enterpriseService.getAllAliveEnterprise(params, keyword, isAlive).then(function (data) {
                $scope.first = $scope.getFirst(params);
                return data;
            });
        }
    });

    $scope.show = function (item) {
            $scope.model = item;
        enterpriseService.getLicenceSendByEnterpriseId(item.id).then(function (data) {
            $scope.licences = data;
            $('#modal-show').modal('show');
        });
        
    }

    $scope.sendSingleLicence = function (model, item) {
        if (!item.file_path||item.file_path=='') {
            alert("没有找到图片信息");
            return;
        }
        if (!item.file_id || item.file_id == '') {
            alert("没有找到图片信息");
            return;
        }
        payingService.sendSingleLicence(model.id, item).then(function (data) {
            $scope.show(model);
        });
    }

    //上传文件
    $scope.uploadFiles = function (files, errFiles, item) {
        $scope.uploading = true;
        if (errFiles.length > 0) {
            swal('有文件不符合要求，无法上传！');
        }

        angular.forEach(files, function (file) {

            var fd = new FormData();
            fd.append("file", file);
            var xhr;
            if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+ ...
                xhr = new XMLHttpRequest();
            } else if (window.ActiveXObject) { // IE 6 and older
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhr.addEventListener('load', function () {
                if (this.status == 200 || this.status == 304) {
                    console.log(xhr.responseText)
                    var data2 = JSON.parse(xhr.responseText);
                    console.log(data2);
                    console.log(item);
                    item.bill_photo_url = data2.data.file_path;
                    console.log(data2.data.file_path)
                    console.log($scope)

                    //           $scope.tableParams2.data[item].bill_photo_url = data2.data.file_path;
                    item.file_id=data2.data.id;
                    item.file_path= data2.data.file_path;
                }

            }, false);
            //xhr.upload.addEventListener('progress', $scope.onProgressHandler, false);
            xhr.open('POST', PHOTO_FILE_URL + '/file', true);
            xhr.send(fd);

        });


    };


});